<?php
class BuyOrderModel extends Db {
	
	private $_buy_order = 'd_buy_order';
	
	public function addBase($params) {
		return $this->add ( $this->_buy_order, $params );
	}
	
	public function getOrderBySNO($sno) {
		return $this->getOne ( $this->_buy_order, array ('orderno' => $sno ) );
	}
	
	public function getOrderByDate() {
		$sql = "select count(id) num,sum(money) money,add_time,from_unixtime(add_time,\"%Y-%m-%d
		\") m from d_buy_order where status IN (1,2,3,4) group by m order by add_time DESC LIMIT 7 ";
		return $this->fetchAll ( $sql );
	}
	
	public function getOrderById($id) {
		return $this->getOne ( $this->_buy_order, array ('id' => $id ) );
	
	}
	
	//根据大于流水号查找成功的订单
	public function getOrderGreaterThanId($id, $status) {
		$sql = "SELECT * FROM $this->_buy_order WHERE id>$id AND status=$status";
		return $this->fetchAll ( $sql );
	}
	
	//订单备注
	public function setOrderRemark($remark, $orderno) {
		return $this->update ( $this->_buy_order, array ('remark' => $remark ), array ('orderno' => $orderno ) );
	}
	
	public function countOrderByUid($uid) {
		$sql = "SELECT id FROM $this->_buy_order WHERE uid=$uid";
		return $this->rowCount ( $sql );
	}
	
	public function countOrderByStatus($status) {
		if ($status === "all") {
			return $this->countOrder ();
		}
		
		$sql = "SELECT id FROM $this->_buy_order WHERE status=$status";
		return $this->rowCount ( $sql );
	}
	
	public function countOrder() {
		$sql = "SELECT id FROM $this->_buy_order";
		return $this->rowCount ( $sql );
	}
	
	public function getOrderByUid($start, $num, $uid) {
		return $this->getOrderPage ( $start, $num, array ('uid' => $uid ) );
	}
	
	public function getOrderByWhere($where, $limit) {
		return $this->getAll ( $this->_buy_order, $where, null, 'id DESC', $limit );
	}
	
	public function getOrderPage($start, $num, $where) {
		return $rs = $this->getPage ( $start, $num, $this->_buy_order, null, $where, 'action_time DESC' );
	}
	
	public function setOrderStatusBySNO($status, $orderno) {
		return $this->update ( $this->_buy_order, array ('status' => $status, 'action_time' => time () ), array ('orderno' => $orderno ) );
	}
	
	public function setOrderById($v, $id) {
		return $this->update ( $this->_buy_order, $v, array ('id' => $id ) );
	}
	
	public function setOrderByOrderSNO($v, $ordersno) {
		return $this->update ( $this->_buy_order, $v, array ('orderno' => $ordersno ) );
	}
	
	public function delOrderById($id) {
		return $this->delete ( $this->_buy_order, array ('id' => $id ) );
	}
	
	public function getOrderByStatus($start, $num, $status) {
		return $this->getOrderPage ( $start, $num, array ('status' => $status ) );
	}
	
	/**
	 * 
	 * @return BuyOrderModel
	 */
	public static function instance() {
		return parent::_instance ( __CLASS__ );
	}

}